<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- google.qdoc -->
<head>
  <title>Using SOAP with the Google API</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
</tr></table><h1 class="title">Using SOAP with the Google API<br /><span class="subtitle"></span>
</h1>
<p>This example shows how to use the SOAP component to submit searches via the Google SOAP API.</p>
<p>Testing this example requires a valid license key, which is available for free from http://api.google.com/.</p>
    <hr>
    <p>examples/google/google.h:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #ifndef GOOGLE_H_INCLUDED
 #define GOOGLE_H_INCLUDED
 #include &lt;QtGui/QWidget&gt;
 #include &lt;qtsoap.h&gt;

 class QLineEdit;
 class QTextBrowser;
 class QPushButton;

 class Google : public QWidget
 {
     Q_OBJECT
 public:
     Google(QWidget *parent = 0);

 private slots:
     void submitRequest();
     void getResponse();

 private:
     QtSoapHttpTransport http;

     QLineEdit *searchString;
     QLineEdit *googleKey;
     QTextBrowser *resultView;
     QPushButton *quitButton;
     QPushButton *searchButton;
     QString key;
 };

 #endif</pre>
    <hr>
    <p>examples/google/google.cpp:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #include &lt;QtGui/QApplication&gt;
 #include &lt;QtGui/QMessageBox&gt;
 #include &lt;QtGui/QCursor&gt;
 #include &lt;QtCore/QFile&gt;
 #include &lt;QtGui/QLabel&gt;
 #include &lt;QtGui/QLineEdit&gt;
 #include &lt;QtGui/QTextBrowser&gt;
 #include &lt;QtGui/QLayout&gt;
 #include &lt;QtGui/QPushButton&gt;
 #include &quot;google.h&quot;

 Google::Google(QWidget *parent)
     : QWidget(parent), http(this)
 {
     <span class="comment">// Generate the GUI.</span>
     quitButton = new QPushButton(&quot;&amp;Quit&quot;, this);
     searchString = new QLineEdit(this);
     resultView = new QTextBrowser(this);
     QVBoxLayout *layout = new QVBoxLayout(this);

     QLabel *gLabel = new QLabel(&quot;Google Web APIs license key: &quot;, this);
     googleKey = new QLineEdit(this);
     QHBoxLayout *topBar = new QHBoxLayout();
     topBar-&gt;addWidget(gLabel);
     topBar-&gt;addWidget(googleKey);
     layout-&gt;addLayout(topBar);

     QLabel *sLabel = new QLabel(&quot;Search string: &quot;, this);
     QPushButton *searchButton = new QPushButton(&quot;Search!&quot;, this);
     QHBoxLayout *searchBar = new QHBoxLayout();
     searchBar-&gt;addWidget(sLabel);
     searchBar-&gt;addWidget(searchString);
     searchBar-&gt;addWidget(searchButton);
     layout-&gt;addLayout(searchBar);

     layout-&gt;addWidget(resultView);
     layout-&gt;addWidget(quitButton);

     <span class="comment">// Connect signals to slots. Note the submitRequest() slots.</span>
     connect(&amp;http, SIGNAL(responseReady()), SLOT(getResponse()));
     connect(quitButton, SIGNAL(clicked()), SLOT(close()));
     connect(searchString, SIGNAL(returnPressed()), SLOT(submitRequest()));
     connect(searchButton, SIGNAL(clicked()), SLOT(submitRequest()));
     connect(googleKey, SIGNAL(returnPressed()), SLOT(submitRequest()));

     <span class="comment">// Pressing enter should trigger a search, unless focus has been</span>
     <span class="comment">// explicitly moved.</span>
     searchButton-&gt;setDefault(true);

     <span class="comment">// Prepare to submit request.</span>
     http.setAction(&quot;urn:GoogleSearchAction&quot;);
     http.setHost(&quot;api.google.com&quot;);
 }

 void Google::submitRequest()
 {
     <span class="comment">// Check that the license key is set.</span>
     if (googleKey-&gt;text() == &quot;&quot;) {
         QMessageBox::warning(this, tr(&quot;Missing license key&quot;),
                              tr(&quot;Please enter your Google Web APIs license key.&quot;
                                 &quot; If you do not have one, you can visit&lt;br&gt;&quot;
                                 &quot; http:<span class="comment">//api.google.com/ and create a Google&quot;</span>
                                 &quot; account to obtain a license key.&quot;));
         return;
     }

     <span class="comment">// Check that we have a search string.</span>
     if (searchString-&gt;text() == &quot;&quot;) {
         QMessageBox::warning(this, tr(&quot;Missing search string&quot;),
                              tr(&quot;Please enter a search string.&quot;));
         return;

     }

     <span class="comment">// Generate request. Details about how to generate a proper</span>
     <span class="comment">// request are available from http://api.google.com/. The results</span>
     <span class="comment">// are always fetched as latin1 in this example, but this can be</span>
     <span class="comment">// easily changed (see the &quot;ie&quot; and &quot;oe&quot; arguments below).</span>
     QtSoapMessage request;
     request.setMethod(QtSoapQName(&quot;doGoogleSearch&quot;, &quot;urn:GoogleSearch&quot;));
     request.addMethodArgument(&quot;key&quot;, &quot;&quot;, googleKey-&gt;text());
     request.addMethodArgument(&quot;q&quot;, &quot;&quot;, searchString-&gt;text());
     request.addMethodArgument(&quot;start&quot;, &quot;&quot;, 0);
     request.addMethodArgument(&quot;maxResults&quot;, &quot;&quot;, 10);
     request.addMethodArgument(&quot;filter&quot;, &quot;&quot;, false, 0);
     request.addMethodArgument(&quot;restrict&quot;, &quot;&quot;, &quot;&quot;);
     request.addMethodArgument(&quot;safeSearch&quot;, &quot;&quot;, false, 0);
     request.addMethodArgument(&quot;lr&quot;, &quot;&quot;, &quot;&quot;);
     request.addMethodArgument(&quot;ie&quot;, &quot;&quot;, &quot;latin1&quot;);
     request.addMethodArgument(&quot;oe&quot;, &quot;&quot;, &quot;latin1&quot;);

     <span class="comment">// Submit the method request to the web service.</span>
     http.submitRequest(request, &quot;/search/beta2&quot;);

     <span class="comment">// Set the cursor to wait mode.</span>
     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 }

 void Google::getResponse()
 {
     <span class="comment">// Set cursor back to normal shape.</span>
     QApplication::restoreOverrideCursor();

     <span class="comment">// Reset resultView.</span>
     resultView-&gt;clear();

     <span class="comment">// Get the response, check for error.</span>
     const QtSoapMessage &amp;resp = http.getResponse();
     if (resp.isFault()) {
         resultView-&gt;setHtml(tr(&quot;&lt;b&gt;Query failed&lt;/b&gt;: &quot;)
                             + resp.faultString().value().toString());
         return;
     }

     <span class="comment">// Extract the return value from this method response, check for</span>
     <span class="comment">// errors.</span>
     const QtSoapType &amp;res = resp.returnValue();
     if (!res.isValid()) {
         resultView-&gt;append(tr(&quot;Invalid return value&quot;));
         return;
     }

     <span class="comment">// Generate resultView output. Make it resemble the actual web</span>
     <span class="comment">// output from http://www.google.com/.</span>
     QString header(tr(&quot;Searched the web for &lt;b&gt;%1&lt;/b&gt;, results %2 - %3 &quot;
                       &quot;of about %4. Search took %5 seconds.&lt;br&gt;&lt;hr&gt;&quot;)
                    .arg(res[&quot;searchQuery&quot;].toString())
                    .arg(res[&quot;startIndex&quot;].toInt())
                    .arg(res[&quot;endIndex&quot;].toInt())
                    .arg(res[&quot;estimatedTotalResultsCount&quot;].toInt())
                    .arg(res[&quot;searchTime&quot;].value().toDouble(), 0, 'f', 2));

     const QtSoapType &amp;resultElements = res[&quot;resultElements&quot;];
     QString allElements;

     for (int i = 0; i &lt; resultElements.count(); ++i) {
         const QtSoapType &amp;resultElement = res[&quot;resultElements&quot;][i];

         QString cat = resultElement[&quot;directoryCategory&quot;][&quot;fullViewableName&quot;].toString();
         QString summary = resultElement[&quot;summary&quot;].toString();
         QString title = resultElement[&quot;title&quot;].toString();
         QString snippet = resultElement[&quot;snippet&quot;].toString();
         QString url = resultElement[&quot;URL&quot;].toString();
         QString cachedSize = resultElement[&quot;cachedSize&quot;].toString();

         QString thisElement = &quot;&lt;br&gt;&quot;;

         if (!title.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#0000FF\&quot;&gt;&lt;b&gt;&lt;u&gt;&quot;
                            + title + &quot;&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         } else {
             thisElement += &quot;&lt;font color=\&quot;#0000FF\&quot;&gt;&lt;b&gt;&lt;u&gt;&quot;
                            + url + &quot;&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         if (!snippet.isEmpty())
             thisElement += snippet + &quot;&lt;br&gt;&quot;;

         if (!summary.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#808080\&quot;&gt;Description:&lt;/font&gt; &quot;
                            + summary + &quot;&lt;br&gt;&quot;;
         }

         if (!cat.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#808080\&quot;&gt;Category: &lt;u&gt;&quot;
                            + cat + &quot;&lt;/u&gt;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         if (!title.isEmpty()) {
             thisElement += &quot;&lt;font color=\&quot;#008000\&quot;&gt;&lt;u&gt;&quot; + url
                            + &quot;&lt;/u&gt; - &quot; + cachedSize + &quot;&lt;/font&gt;&lt;br&gt;&quot;;
         }

         allElements += thisElement;
     }

     <span class="comment">// Update the resultView.</span>
     resultView-&gt;setHtml(header + allElements);
 }</pre>
    <hr>
    <p>examples/google/main.cpp:</p>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the Qt Solutions component.
 **
 ** You may use this file under the terms of the BSD license as follows:
 **
 ** &quot;Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
 ** met:
 **   * Redistributions of source code must retain the above copyright
 **     notice, this list of conditions and the following disclaimer.
 **   * Redistributions in binary form must reproduce the above copyright
 **     notice, this list of conditions and the following disclaimer in
 **     the documentation and/or other materials provided with the
 **     distribution.
 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
 **     the names of its contributors may be used to endorse or promote
 **     products derived from this software without specific prior written
 **     permission.
 **
 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
 **
 ****************************************************************************/</span>

 #include &lt;QtGui/QApplication&gt;
 #include &quot;google.h&quot;

 int main(int argc, char **argv)
 {
     QApplication app(argc, argv);

     Google google;

     google.show();

     return app.exec();
 }</pre>
    <hr>
    <p>examples/google/google.pro:</p>
<pre> TEMPLATE = app
 INCLUDEPATH += .

 include(../../src/qtsoap.pri)

 # Input
 HEADERS += google.h
 SOURCES += google.cpp main.cpp</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
</tr></table></div></address></body>
</html>
